/* 
 * LoggingFacade.java 02.12.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.util;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * Facade class for logging mechanisms. 
 * 
 * @author Alessandra Otterbach
 *
 */
public class LoggingFacade {

    /** Class for which this instance of LoggingFacade should be created. */
    private Class<?> clazz;

    private Logger logger = Logger.getLogger("UrlyBird");

    private static final String LOGGER_NAME = "UrlyBird";

    private LoggingFacade(Class<?> clazz) {

	this.clazz = clazz;
	this.logger = Logger.getLogger(LOGGER_NAME + "." + clazz.getName());

	logger.setLevel(Level.OFF);

	// exit default consoleHandler
	logger.setUseParentHandlers(false);

	ConsoleHandler ch = new ConsoleHandler();
	ch.setLevel(Level.FINE);
	ch.setFormatter(new SimpleFormatter());
	logger.addHandler(ch);
    }

    /**
     * Creating a LoggingFacade for the given class.
     * 
     * @param clazz
     * @return LoggingFacade
     */
    public static synchronized LoggingFacade getInstance(Class<?> clazz) {
	return new LoggingFacade(clazz);
    }

    /**
     * Logging info-messages
     * 
     * @param methodName calling method name
     * @param message additional message
     */
    public synchronized void info(String methodName, String message) {
	logger.logp(Level.INFO, clazz.getName(), methodName, message);
    }

    /**
     * Logging debug-messages
     * 
     * @param methodName calling method name
     * @param message additional message
     */
    public synchronized void debug(String methodName, String message) {
	logger.logp(Level.FINER, clazz.getName(), methodName, message);
    }
    
    /**
     * Logging error-messages
     * 
     * @param methodName calling method name
     * @param message additional message
     */
    public synchronized void error(String methodName, String message) {
	logger.logp(Level.WARNING, clazz.getName(), methodName, message);
    }

    /**
     * Setting the loglevel
     * 
     * @param level loglevel to be set
     */
    public void setLevel(Level level) {
	logger.setLevel(Level.OFF);
    }
}
